package org.jim.server.command.handler;

import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.config.ImConfig;
import org.jim.core.exception.ImException;
import org.jim.core.packets.*;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager;
import org.jim.server.util.ImServerKit;

/**
 *
 */
public class HeartbeatReqHandler extends AbstractCmdHandler {
    @Override
    public ImPacket handler(ImPacket packet, ImChannelContext channelContext) throws ImException {
        RespBody heartbeatBody = new RespBody(Command.COMMAND_HEARTBEAT_REQ).setData(new HeartbeatBody(Protocol.HEARTBEAT_BYTE));
        ImPacket heartbeatPacket = ProtocolManager.Converter.respPacket(heartbeatBody, channelContext);
        User user = ImServerKit.getUser(channelContext);
        //接收心跳，更新在线状态
        ImServerConfig imConfig = ImConfig.Global.get();
        imConfig.getMessageHelper().updateUserTerminal(User
                .newBuilder()
                .userId(user.getUserId())
                .terminal(user.getTerminal())
                .status(UserStatusType.ONLINE.getStatus())
                .build());
        return heartbeatPacket;
    }

    @Override
    public Command command() {
        return Command.COMMAND_HEARTBEAT_REQ;
    }
}
